package reader

import (
	"encoding/hex"
	"github.com/lishimeng/go-log"
	"time"
)

//
func setPower(session Reader, pow uint) (err error) {

	log.Debug("config power %d", pow)
	power := byte(pow)
	if power > 0x21 {
		power = 0x21
	}
	//p := make([]byte, 3)
	p := make([]byte, 6)
	p[0] = 0x01 // addr
	p[1] = 0x76
	p[2] = power
	p[3] = power
	p[4] = power
	p[5] = power
	session.writeSerialBody(p)
	time.Sleep(time.Millisecond * 100)

	// receive
	packetSize := session.readPacketHead()

	packetBody := session.readPacketBody(packetSize)

	if packetBody[2] != Success {
		log.Info("set power error " + hex.EncodeToString(packetBody[2:3]))
		panic("set power error " + hex.EncodeToString(packetBody[2:3]))
	}

	return err
}

func selectAntenna(session Reader, antenna uint) (err error) {

	log.Debug("select antenna")
	p := make([]byte, 3)
	p[0] = 0x01 // addr
	p[1] = 0x74
	p[2] = byte(antenna)
	session.writeSerialBody(p)
	time.Sleep(time.Millisecond * 50)

	// receive
	packetSize := session.readPacketHead()

	packetBody := session.readPacketBody(packetSize)

	if packetBody[2] != Success {
		log.Info("select antenna error " + hex.EncodeToString(packetBody[2:3]))
		panic("select antenna error " + hex.EncodeToString(packetBody[2:3]))
	}

	return err
}

func inventoryStart(session Reader) {

	log.Debug("invoke inventory")
	p := make([]byte, 3)
	p[0] = 0x01 // addr
	p[1] = 0x89
	p[2] = 0xFF
	session.writeSerialBody(p)
	//time.Sleep(time.Millisecond * 100)
}

func fastInventory(session Reader) {
	log.Debug("invoke fast inventory")

	var times byte = 0x03
	var antennaSleep byte = 0x10
	var repeat byte = 3
	p := make([]byte, 12)
	p[0] = 0x01 // addr
	p[1] = 0x8A
	p[2] = 0xFF// A
	p[3] = times
	p[4] = 0xFF // B
	p[5] = times
	p[6] = 0xFF// C
	p[7] = times
	p[8] = 0xFF // D
	p[9] = times
	p[10] = antennaSleep
	p[11] = repeat

	for _, antenna := range session.opts.Antennas {
		if antenna == 0 {
			p[2] = 0x00
		}
		if antenna == 1 {
			p[4] = 0x01
		}
		if antenna == 2 {
			p[6] = 0x02
		}
		if antenna == 3 {
			p[8] = 0x03
		}
	}
	session.writeSerialBody(p)
	//time.Sleep(time.Millisecond * 100)
}

func handleInventory(session Reader) (res InventoryInfo) {

	log.Debug("handle inventory")
	res = InventoryInfo{}
	packetSize := session.readPacketHead()

	log.Debug("packet size: %d", packetSize)
	switch packetSize {
	case 0x04:// 异常
		packetBody := session.readPacketBody(packetSize)
		log.Debug("Inventory error " + hex.EncodeToString(packetBody[2:3]))
		//panic("Inventory error " + hex.EncodeToString(packetBody[2:3]))
		res.PacketType = InventoryError
	case 0x0A:// 结束包
		res.PacketType = TagSummaryType

		packetBody := session.readPacketBody(packetSize)

		summary := TagSummary{}
		summary.AntID = packetBody[2]
		summary.ReadRate = packetBody[3:5]
		summary.TotalRead = packetBody[5:9]
		res.TagSummary = summary
	default:// 标签包
		res.PacketType = TagInfoType
		packetBody := session.readPacketBody(packetSize)

		tagInfo := TagInfo{}
		tagInfo.FreqAnt = packetBody[2]
		tagInfo.PC = packetBody[3:5]
		// epc长度算法: PC[0]>>3(WORD)
		var epcSize = ((tagInfo.PC[0]) >> 3)*2
		epcRaw := packetBody[5:packetSize - 2]
		tagInfo.EPC = epcRaw[:epcSize]
		tagInfo.TID = epcRaw[epcSize:]
		tagInfo.RSSI = packetBody[packetSize - 2]

		tagInfo.EpcText = hex.EncodeToString(tagInfo.EPC)
		if len(tagInfo.TID) > 0 {
			tagInfo.TidText = hex.EncodeToString(tagInfo.TID)
		}

		res.TagInfo = tagInfo
	}
	log.Debug("packet type: %d", res.PacketType)

	return res
}